<template>
  <div class="order-container"> <!-- 将 el-card 替换为 div -->
    <div class="header"> <!-- 直接使用 div 作为标题部分 -->
      <!-- <h1>养老院列表</h1> -->
    </div>
    <el-row
      type="flex"
      justify="start"
      class="card-wrap"
      :gutter="4"
    >
      <el-col
       
        v-for="(item, index) in state.tableData"
        :key="index"
        :xs="24"
        :sm="12"
        :md="6"
        :lg="6"
        class="card-col"
      >
        <el-card
          :body-style="cardBodyStyle"
          class="card-item"
        >
          <img
            :src="item.image"
            alt="图片"
            class="image"
            @click="handleOpenDialog(item.id)"
          />
          <div style="padding: 8px">
            <p>{{ item.name }}&emsp;&emsp;&emsp;<span style="color: red">{{ item.price }} 元/月</span></p>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="state.total"
      :page-size="state.pageSize"
      :current-page="state.currentPage"
      @current-change="changePage"
    />
  </div>
  <NuringListDialog
    ref="infoClassify"
    title="具体详情"
    :height="dialogHeight"
    width="40%"
  />

</template>

<script setup>
import { onMounted, reactive, ref, watchEffect, onUnmounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { clientlistNursingHomes } from '../../service/nursingHomeApi.js';
import NuringListDialog from '../client/NuringListDialog.vue';
import { deleteNursingHomeById } from '../../service/nursingHomeApi.js'; // 假设存在这个删除接口

const infoClassify = ref(null);
const dialogHeight = ref('');

// 数据列表
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 8, // 分页大小
  id: ''
});

// 初始渲染
onMounted(() => {
  getIndexConfig();
  updateDialogHeight();
  window.addEventListener('resize', updateDialogHeight);
});

const handleOpenDialog = (id) => {
  infoClassify.value.open(id);
};

// 获取列表数据（添加搜索参数）
const getIndexConfig = async () => {
  state.loading = true;
  const params = {
    pageNum: state.currentPage,
    pageSize: state.pageSize
  };
  // 查询资讯分类列表数据
  const response = await clientlistNursingHomes(params);
  state.tableData = response.data.records || [];
  state.total = response.data.total || 0;
  state.loading = false;
};

// 翻页方法
const changePage = (val) => {
  state.currentPage = val;
  getIndexConfig();
};


// 更新对话框高度
const updateDialogHeight = () => {
  const windowHeight = window.innerHeight;
  // 这里可以根据需要调整对话框高度占页面高度的比例，例如 80%
  dialogHeight.value = `${windowHeight * 0.8}px`;
};

// 组件卸载时移除事件监听器
onUnmounted(() => {
  window.removeEventListener('resize', updateDialogHeight);
});
</script>

<style scoped>
/* 外层容器 */
.order-container {
  width: 100%;
  max-width: 1225px;
  margin: 0 auto;
  /* padding: 0 10px; 添加容器边距 */
}

/* 标题部分样式 */
.header {
  text-align: center;
  margin-bottom: 20px;
}

/* 卡片容器 */
.card-wrap {
  flex-wrap: wrap;
  margin: 0 -2px; /* 抵消列间距 */
}

/* 卡片列 */
.card-col {
  margin-bottom: 20px;
  /* margin-left: 10px; */
  padding: 0 2px !important; /* 列间距 */
  display: flex;
}

/* 卡片主体 */
.card-item {
  width: 290px; /* 改为100%宽度 */
  border-radius: 6px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.image {
  width: 100%;
  height: 220px;
  border-radius: 2px;
}

/* 其他样式保持原有修改 */
.time {
  font-size: 12px;
  color: #666;
}

.bottom {
  margin-top: 8px;
  line-height: 16px;
}

.button {
  padding: 0;
  font-size: 12px;
}

.footer {
  height: 100px; /* 根据需要调整高度 */
  border-top: 1px solid #e9e9e9;
  display: flex;
  flex-direction: column; /* 垂直排列 */
  justify-content: center;
  align-items: center;
  padding: 0 20px;
}
</style>